local MOONFIRE_COST = 40
local MOONFIRE_COOLDOWN = 5

local function EndLunarFire(fx, doer)
    if doer.components.channelcaster then
        doer.components.channelcaster:StopChanneling()
    end
    if doer.components.locomotor ~= nil then
        if doer.components.grogginess ~= nil and not doer.components.grogginess.isgroggyl then
            doer.components.locomotor:RemoveExternalSpeedMultiplier(doer, "grogginess")
        end
    end
end

local function TryLunarFire(doer)
    if doer.components.channelcaster and not doer.components.channelcaster:IsChanneling() then
        local fx = SpawnPrefab("flamethrower_fx")
        if fx and fx.components.weapon then
            fx.components.weapon:SetDamage(20)
        end
        if fx and fx.components.planardamage then
            fx.components.planardamage:SetBaseDamage(30)
        end
        fx.entity:SetParent(doer.entity)
        if fx.SetFlamethrowerAttacker then
            fx:SetFlamethrowerAttacker(doer)
        end

        local endtask = fx:DoTaskInTime(MOONFIRE_COOLDOWN, EndLunarFire, doer)
        fx:ListenForEvent("stopchannelcast", function()
            if fx then
                endtask:Cancel()
                fx:KillFX()
            end
        end, doer)

        if doer.components.channelcaster:StartChanneling() then
            return true
        end
    end
    return false
end

local function CanCastMoonFire(doer)
    return
        doer.components.sanity ~= nil
        and doer.components.sanity:GetCurrent() >= MOONFIRE_COST
        and (doer._moonfire_cd == nil or GetTime() > doer._moonfire_cd)
end

local function CastMoonFire(doer)
    if not CanCastMoonFire(doer) then
        if doer.components.talker then
            doer.components.talker:Say("理智值不够或技能冷却中")
        end
        return
    end
    if TryLunarFire(doer) then
        doer.components.sanity:DoDelta(-MOONFIRE_COST)
        doer._moonfire_cd = GetTime() + MOONFIRE_COOLDOWN
        if doer.components.talker then
            doer.components.talker:Say("月火！")
        end
    end
end

-- 组件定义
local MoonFireCaster = Class(function(self, inst)
    self.inst = inst
    if TheWorld.ismastersim then
        inst:ListenForEvent("moonfire_cast", function()
            CastMoonFire(inst)
        end)
    end
end)

-- 注册组件
AddComponentPostInit("player_classified", function(inst)
    if inst and inst._parent and not inst._parent.components.moonfirecaster then
        inst._parent:AddComponent("moonfirecaster")
    end
end)

-- RPC注册
local MOD_RPC_NAMESPACE = "moonfire"
local MOD_RPC_EVENT = "cast"
AddModRPCHandler(MOD_RPC_NAMESPACE, MOD_RPC_EVENT, function(inst)
    if inst and inst.components.moonfirecaster then
        inst:PushEvent("moonfire_cast")
    end
end)

-- 客户端监听R键
if TheNet:GetIsClient() then
    local KEY_R = 114 -- R键keycode
    local pressed = false

    TheInput:AddKeyDownHandler(KEY_R, function()
        if not pressed and ThePlayer and ThePlayer:HasTag("pansy") then
            pressed = true
            SendModRPCToServer(MOD_RPC["moonfire"]["cast"])
        end
    end)
    TheInput:AddKeyUpHandler(KEY_R, function()
        pressed = false
    end)
end

return MoonFireCaster
